package com.xyzh.application.base.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xyzh.application.common.util.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.ReflectionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动补充插入或更新时的值
 * 
 */
@Component
public class CustomMetaObjectHandler implements MetaObjectHandler {
    private static final Logger log = LoggerFactory.getLogger(CustomMetaObjectHandler.class);
 
    public CustomMetaObjectHandler() {
    }
 
    @Override
    public void insertFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("createby", SecurityUtils.getUserId(), metaObject);
            this.setFieldValByName("createtime", new Date(), metaObject);
        } catch (ReflectionException var3) {
            log.warn("CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }
 
    }
 
    @Override
    public void updateFill(MetaObject metaObject) {
        try {
            this.setFieldValByName("updateby", SecurityUtils.getUserId(), metaObject);
            this.setFieldValByName("updatetime", new Date(), metaObject);
        } catch (ReflectionException var3) {
            log.warn("CustomMetaObjectHandler处理过程中无相关字段，不做处理");
        }
 
    }
}